Public Partial Class FrmListadoRecibos
    Inherits System.Web.UI.Page
    Public Facturas As CapaDeNegocios.nFactura = New CapaDeNegocios.nFactura
    Public Cliente As CapaDeNegocios.nCliente = New CapaDeNegocios.nCliente
    Public Recibo As CapaDeNegocios.nRecibo = New CapaDeNegocios.nRecibo
    Public DetalleRecibo As CapaDeNegocios.nDetalleRecibo = New CapaDeNegocios.nDetalleRecibo

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If

        If Not Page.IsPostBack Then
            LLenarCboClientes()
            BtnImprimir.OnClientClick = "javascript:var popup = window.open('../Reportes/RptListadoDeRecibos.aspx',(new Date().getTime()),'Width=900,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"

            BtnImprimir.Visible = False
        End If
    End Sub
    Sub LLenarCboClientes()
        CboClientes.Items.Clear()
        CboClientes.Items.Add(New ListItem("Ninguno...", -1))
        CboClientes.DataSource = Cliente.Listar("", "", "", "", "", "", "", "", "", "", "", "")
        CboClientes.DataTextField = "RazonSocial"
        CboClientes.DataValueField = "CodigoCliente"
        CboClientes.DataBind()
        If CboClientes.Items.Count > 0 Then
            CboClientes.SelectedIndex = 0
        End If
    End Sub

    Protected Sub LnkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkBuscar.Click
        Dim dts As DataSet
        If CboClientes.SelectedValue.ToString() <> "-1" Then
            dts = Recibo.BuscarFiltrado("", "", "", "", CboClientes.SelectedValue.ToString())
        Else
            dts = Recibo.BuscarFiltrado("", "", "", "", "")
        End If

        GridRecibo.DataSource = dts
        GridRecibo.DataBind()
        If dts.Tables(0).Rows.Count > 0 Then
            BtnImprimir.Visible = True
            Session("RptListadoDeRecibos") = dts
        Else
            BtnImprimir.Visible = False
        End If
    End Sub

    Protected Sub GridRecibo_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridRecibo.RowCommand
        If e.CommandName = "Anular" Then
            Dim IdRecibo As String = e.CommandSource.Parent.Parent.FindControl("HFIdRecibo").Value.ToString()
            If e.CommandSource.Parent.Parent.FindControl("LblEstado").text.ToString() = "ACTIVO" Then
                Dim dtsDetalleRecibo As DataSet = DetalleRecibo.BuscarFiltrado("", IdRecibo, "")
                For Each row As DataRow In dtsDetalleRecibo.Tables(0).Rows
                    Facturas.Modificar(row("IdFactura").ToString(), "", "", "EMITIDA", "", "", "", "", "", "")
                Next
                Recibo.ModificarEstado(IdRecibo, "ANULADO")
                LnkBuscar_Click(sender, e)
            End If
        End If
    End Sub

    Protected Sub GridRecibo_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridRecibo.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim IntLnkSeleccion As LinkButton = e.Row.FindControl("LnkSeleccion")
            Dim IntLnkImputaciones As LinkButton = e.Row.FindControl("LnkImputaciones")
            Dim IntLblEstado As Label = e.Row.FindControl("LblEstado")
            Dim IntLnkAnular As LinkButton = e.Row.FindControl("LnkAnular")
            Dim IntHFIdRecibo As HiddenField = e.Row.FindControl("HFIdRecibo")
            IntLnkSeleccion.OnClientClick = "javascript:var popup = window.open('FrmListadoDetalleDeRecibos.aspx?IDRecibo=" + IntHFIdRecibo.Value.ToString() + "',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
            IntLnkImputaciones.OnClientClick = "javascript:var popup = window.open('FrmListadoImputacionesRecibo.aspx?IDRecibo=" + IntHFIdRecibo.Value.ToString() + "',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
            IntLnkAnular.Visible = IntLblEstado.Text.ToString() = "ACTIVO"
        End If
    End Sub


    Protected Sub GridRecibo_PageIndexChanging(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridRecibo.PageIndexChanging
        GridRecibo.PageIndex = e.NewPageIndex
        LnkBuscar_Click(sender, e)
    End Sub
End Class